package com.woniu.carrent.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.carrent.mapper.TCarMapper;
import com.woniu.carrent.mapper.TCardetailMapper;
import com.woniu.carrent.mapper.TCarphotoMapper;
import com.woniu.carrent.pojo.TCar;
import com.woniu.carrent.pojo.TCardetail;
import com.woniu.carrent.pojo.dto.TCarDto;
import com.woniu.carrent.pojo.vo.TCarTypecVo;
import com.woniu.carrent.pojo.vo.TCarVo;
import com.woniu.carrent.service.TCarService;
import com.woniu.carrent.util.ResultMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tcar")
@CrossOrigin
@SaCheckLogin
public class TCarController {
    @Autowired
    private TCarService tCarService;
    @Autowired
    private TCardetailMapper tCardetailMapper;
@Autowired
private TCarphotoMapper tCarphotoMapper;
@Autowired
private TCarMapper tCarMapper;
    /**
     * 条件查询车辆&查询全部
     * @param tcarDto
     * @return
     */
    @RequestMapping("/selectCars")
    public ResultMessage selectCars(@RequestBody TCarDto tcarDto){
        Integer pageNum = tcarDto.getPageNum();
        Integer pageSize = tcarDto.getPageSize();

        Page<TCar> page = new Page<>(pageNum, pageSize);
        List<TCar> list = tCarService.selectCars(page, tcarDto);

        Map<String, Object> result = new HashMap<>();
        result.put("records", list); // 查询数据
        result.put("total", page.getTotal()); // 数据总条数
        result.put("current", page.getCurrent()); // 当前页
        result.put("pages", page.getPages()); // 总页数
        result.put("size", page.getSize()); // 页大小

        if (list != null && !list.isEmpty()){
            return ResultMessage.success(result);
        }
        return ResultMessage.fail("获取失败");

    }
    /**
     * 添加车辆
     * @param tcarDto
     * @return
     */
    @RequestMapping("/insertCar")
    public ResultMessage insertCar(@RequestBody TCarDto tcarDto){
        TCar car = new TCar();
        BeanUtils.copyProperties(tcarDto,car);
        boolean flag = tCarService.save(car);
        if (flag){
            return ResultMessage.success("添加成功");
        }
        return ResultMessage.fail("添加失败");
    }
    /**
     * 修改车辆信息
     * @param tcarDto
     * @return
     */
    @RequestMapping("/updateCar")
    public ResultMessage updateCar(@RequestBody TCarDto tcarDto){
        TCar car = new TCar();
        BeanUtils.copyProperties(tcarDto,car);
        boolean flag = tCarService.removeById(car);
        if (flag){
            return ResultMessage.success("修改成功");
        }
        return ResultMessage.fail("修改失败");
    }
    /**
     * 删除车辆
     * @param tcarDto
     * @return
     */
    @RequestMapping("/deleteCar")
    public ResultMessage deleteCar(@RequestBody TCarDto tcarDto){
        TCar car = new TCar();
        BeanUtils.copyProperties(tcarDto, car);

        if (car.getIsdeleted() == null) {
            // 先查询车辆详情
            List<TCardetail> cardetails = tCardetailMapper.selectcardetail(car.getCarid());

            // 删除所有相关照片
            for (TCardetail cardetail : cardetails) {
                tCarphotoMapper.deleteByCdId(cardetail.getCdid());
            }

            // 删除车辆详情
            tCardetailMapper.deleteByCarId(car.getCarid());

            // 删除车辆
            boolean carDeleted = tCarService.removeById(car);

            if (carDeleted) {
                return ResultMessage.success("删除成功");
            }
        }

        return ResultMessage.fail("删除失败");
    }

    /**
     * 查询车辆详情
     * @param carId
     *  @return
     */
    @RequestMapping("/selectcardetail")
    public ResultMessage selectcardetail(Integer carId){
        List<TCardetail> tcar = tCardetailMapper.selectcardetail(carId);
        if (tcar != null){
            return ResultMessage.success(tcar);
        }
        return ResultMessage.fail("查询失败");
    }
    /**
     * 修改车辆详情
     * @param tcarDateil
     * @return
     */
    @RequestMapping("/updateCarDetail")
    public ResultMessage updateCardetail(@RequestBody TCardetail tcarDateil){
        int flag = tCardetailMapper.updateById(tcarDateil);
        if (flag>0){
            return ResultMessage.success("修改成功");
        }
        return ResultMessage.fail("修改失败");
    }
    /**
     * 根据商家的id查询所有车辆
     */
    @RequestMapping("/selectCarBySellerId")
    public ResultMessage selectCarBySellerId(Integer sellerId){
        List<TCarTypecVo> list = tCarMapper.selectCarBySellerId(sellerId);
        return ResultMessage.success(list);
    }

}
